package com.yg.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yg.config.result.R;
import com.yg.entity.singleEntity.QualityCourse;
import com.yg.service.QualityCourseService;
import com.yg.utils.DateValidationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 对应sheet06，此表是质量工程表课程，对应着表中的课程内容 前端控制器
 * </p>
 *
 * @author YingGu
 * @since 2020-11-04
 */
@RestController
@RequestMapping("/qualityCourse")
@CrossOrigin
@Slf4j
@Api(tags = "对应第六张表")
public class QualityCourseController {

    @Autowired
    private QualityCourseService qualityCourseService;

    /**
     * 添加数据
     *
     * @param qualityCourse
     * @return
     */
    @ApiOperation("添加数据")
    @PostMapping("postQualityCourse")
    private R postQualityCourse(
            @ApiParam(name = "质量工程课程数据") @RequestBody(required = true) QualityCourse qualityCourse
    ) {
        qualityCourse.setTime(DateValidationUtil.dateValidation(qualityCourse.getTime(), "yyyy.MM"));
        try {
            boolean flag = qualityCourseService.save(qualityCourse);
            return flag ? R.OK() : R.ERROR();
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("添加失败");
        }
    }

    /**
     * 获取数据
     *
     * @return
     */
    @ApiOperation("获取数据")
    @GetMapping("getAllQualiyuCourse")
    private R getAllQualityCourse(
            @ApiParam(name = "current", value = "当前页", required = true) @RequestParam(name = "current") long current,
            @ApiParam(name = "limit", value = "每页显示的条目数", required = true) @RequestParam(name = "limit") long limit
    ) {

        try {
            Page<QualityCourse> page = new Page<>(current, limit);

            QueryWrapper<QualityCourse> wrapper = new QueryWrapper<>();
            wrapper.eq("deleted", 0);

            qualityCourseService.page(page, wrapper);
            List<QualityCourse> list = page.getRecords();
            long total = page.getTotal();

            long pages = page.getPages();

            return R.OK().data("qualityCourseList", list).data("total", total).data("pages", pages);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("查询失败");
        }
    }

    /**
     * 条件获取数据
     *
     * @return
     */
    @ApiOperation("条件获取数据")
    @PostMapping("selectQualityCourseByCondition")
    private R selectQualityCourseByCondition(
            @ApiParam(name = "current", value = "当前页", required = false) @RequestParam(name = "current") long current,
            @ApiParam(name = "limit", value = "每页显示的条目数", required = false) @RequestParam(name = "limit") long limit,
            @ApiParam(name = "qualityCourse", value = "条件", required = false) @RequestBody(required = false) QualityCourse qualityCourse
    ) {
        try {
            Page<QualityCourse> page = new Page<>(current, limit);

            QueryWrapper<QualityCourse> wrapper = null;
            wrapper = new QueryWrapper<>();
            wrapper.eq("deleted", 0);

            if (!StringUtils.isEmpty(qualityCourse.getName())) {
                wrapper.like("name", qualityCourse.getName());
            }
            if (!StringUtils.isEmpty(qualityCourse.getLevel())) {
                wrapper.eq("level", qualityCourse.getLevel());
            }

            qualityCourseService.page(page, wrapper);

            List<QualityCourse> list = page.getRecords();
            long total = page.getTotal();
            long pages = page.getPages();

            return R.OK().data("qualityCourseList", list).data("total", total).data("pages", pages);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("查询失败");
        }
    }

    /**
     * 修改数据
     *
     * @return
     */
    @ApiOperation("修改数据")
    @PutMapping("putQualityCourse")
    private R putQualityCourse(
            @ApiParam(name = "qualityCourse", value = "修改后的课程数据", required = false) @RequestBody QualityCourse qualityCourse
    ) {
        QueryWrapper<QualityCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("id", qualityCourse.getId());

        qualityCourse.setTime(DateValidationUtil.dateValidation(qualityCourse.getTime(),"yyyy.MM"));
        boolean flag = qualityCourseService.update(qualityCourse, wrapper);
        return flag ? R.OK() : R.ERROR().message("修改失败");
    }

    /**
     * 删除质量工程课程数据
     *
     * @return
     */
    @ApiOperation("删除质量工程课程数据")
    @DeleteMapping("deleteQualityCourse")
    private R deleteQualityCourse(
            @ApiParam(name = "qualityCourse", value = "课程数据", required = false) @RequestBody QualityCourse qualityCourse
    ) {
        String id = null;
        try {
            id = qualityCourse.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("查询信息错误");
        }

        try {
            boolean flag = qualityCourseService.removeById(id);
            return flag ? R.OK() : R.ERROR();
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("删除失败");
        }
    }

    /**
     * 表格读取到数据库
     *
     * @param file sheet06表
     * @return
     */
    @ApiOperation("表格读取到数据库")
    @PostMapping("readExcel")
    private R readExcel(MultipartFile file) {

        R r = R.OK();

        qualityCourseService.readExcel(file, r);
        return r;
    }

    /**
     * 表格导出
     */
    @ApiOperation("表格导出")
    @GetMapping("writeExcel")
    private void writeExcel(
            HttpServletResponse response
    ) {
        qualityCourseService.writeExcel(response);
    }
}